package cn.szzsi._admin;

import cn.szzsi.service.UserBalanceDetailService;
import cn.szzsi.service.UserBalanceService;
import cn.szzsi.service.UserService;
import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.excel.PoiRender;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.SessionUtil;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Tocash;
import cn.szzsi.common.model.UserBalanceDetail;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 处理买家提现申请
 * Created by yuanzhneg on 2016/11/26.
 */
public class ToCashingController extends BaseAdminController<Tocash> {
    private String shopId;
    public void list(){
        shopId=getAttr("shopId");
        int pageNumber=getParaToInt("pageNumber", 1);
        int pageSize=getParaToInt("pageSize", 10);
        StringBuilder sql=new StringBuilder("from mall_tocash where uid='"+shopId+"' and status = 0");
        sql.append(" order by createDate desc ");
        Page<Tocash> pager = Tocash.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
        setAttr("pager", pager);
        setAttr("title","最新提现管理");
        render("tocashing_manage_list.html");

    }

    public void test(){
        ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfig("C1DFFAD80E4F467B3924BEE2F53ED139"));
        UserService.us.sendToCashingMsg(Member.dao.findById("9a112e30d1644d549d8d9dd0954d8d83"),new BigDecimal(100));
        UserService.us.sendToCashingSucessMsg(Member.dao.findById("9a112e30d1644d549d8d9dd0954d8d83"),"100","中国银行");
        ApiConfigKit.removeThreadLocalApiConfig();
    }

    //提现导出
    public void export(){
        String type = getPara("type");
        String sql = "";
        String name = "";
        if(type.equalsIgnoreCase("now")){
            sql = "and status = 0";
            name = "cash_apply.xls";
        }else if(type.equalsIgnoreCase("history")){
            sql = "and status = 1";
            name = "cash_history.xls";
        }
        List<Record> list = Db.find("select bb.name,b.cashAmount,b.createDate,bb.cardNumber,bb.cardCount,bb.cardAddress from mall_tocash b left join mall_tocash_bank bb on b.bankId = bb.id where 1=1 "+sql+" order by b.createDate desc");
        String[] header={"申请人","银行卡号","开户行","开户行地址","提现金额","提现日期"};
        String[] columns={"name","cardNumber","cardCount","cardAddress","cashAmount","createDate"};
        //其实关键代码就这一句话fileName、sheetName、columns、headers为必须
        Render poirender = PoiRender.me(list).fileName(name).headers(header).sheetName("提现信息").columns(columns);
        render(poirender);
    }

    //历史提现
    public void history(){
        shopId=getAttr("shopId");
        int pageNumber=getParaToInt("pageNumber", 1);
        int pageSize=getParaToInt("pageSize", 10);
        StringBuilder sql=new StringBuilder("from mall_tocash where uid='"+shopId+"' and status != 0");
        sql.append(" order by createDate desc ");
        Page<Tocash> pager = Tocash.dao.paginate(pageNumber,pageSize,"select * ",sql.toString());
        setAttr("pager", pager);
        setAttr("title","历史提现管理");
        render("tocashing_manage_history.html");

    }

    public void ajaxlist(){
        shopId=getAttr("shopId");
        int status=getParaToInt("status");
        String sql = StringUtils.EMPTY;
        if(status == 0){//提现中
            sql = "select bb.*,m.username as name,m.phone as phone,m.code from (select bt.*,btb.name as realname,btb.cardNumber as cardNumber,btb.cardCount as cardCount,btb.cardAddress as cardAddress from mall_tocash bt left join mall_tocash_bank btb on bt.bankId = btb.id  where bt.uid = ? and bt.status=?) bb left join mall_member m on bb.member_id = m.id order by bb.createDate desc";
        }else if(status == 1){//提现通过和不通过
            status = 0;
            sql = "select bb.*,m.username as name,m.phone as phone,m.code from (select bt.*,btb.name as realname,btb.cardNumber as cardNumber,btb.cardCount as cardCount,btb.cardAddress as cardAddress from mall_tocash bt left join mall_tocash_bank btb on bt.bankId = btb.id  where bt.uid = ? and bt.status!=?) bb left join mall_member m on bb.member_id = m.id order by bb.createDate desc";
        }
        List<Tocash> lpa = Tocash.dao.find(sql,shopId,status);
        renderJson(lpa);
    }

    //处理买家提现
    public void  process(){
        shopId=getAttr("shopId");
        int id =getParaToInt("id");//提现记录id
        int type = getParaToInt("type");
        String reason = StringUtils.trim(getPara("reason"));

        BigDecimal cashAmount=new BigDecimal(getPara("cashAmount"));//提现金额
        Tocash toCashing= Tocash.dao.findById(id);//
        if(toCashing.getInt("status")!= Tocash.ToCashingStatus.inToCashing.ordinal()){
            renderJson(Msg.fail("该订单已处理哦!",""));
            return;
        }

        String memberId=toCashing.getStr("member_id");
        Member member = Member.dao.findById(memberId);
        if(type == 0){
            if(StrKit.notBlank(reason)){
                toCashing.set("reason",reason);
            }
            toCashing.set("status", Tocash.ToCashingStatus.reject.ordinal()).update();
            //发送提现成功模板消息
            //ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfig("C1DFFAD80E4F467B3924BEE2F53ED139"));
            //UserService.us.sendToCashingFailMsg(member,toCashing);
            //ApiConfigKit.removeThreadLocalApiConfig();
        }else{
            toCashing.set("status", Tocash.ToCashingStatus.toCashingSuccess.ordinal()).update();//更新提现状态

            UserBalanceService.service.updateCashing(member,cashAmount);//更新会员余额结果
            UserBalanceDetailService.service.saveToCashing(memberId,cashAmount, UserBalanceDetail.BalanceType.memberToCashing.ordinal(),shopId);//增加提现流水号
            //发送提现成功模板消息
            //ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfig("C1DFFAD80E4F467B3924BEE2F53ED139"));
            //UserService.us.sendToCashingMsg(member,cashAmount);
            //ApiConfigKit.removeThreadLocalApiConfig();
        }

        renderJson(Msg.SUCCESS);

    }
}
